package org.dromara.system.controller.yzl;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.YzlSafetyCameraBo;
import org.dromara.system.domain.vo.YzlSafetyCameraVo;
import org.dromara.system.domain.vo.YzlShieldDeviceVo;
import org.dromara.system.domain.vo.YzlYs7CameraVo;
import org.dromara.system.service.IYzlSafetyCameraService;
import org.dromara.system.service.IYzlShieldDeviceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 摄像头视频
 *
 * @author fxnn
 * @date 2025-09-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/safetyCamera")
public class YzlSafetyCameraController extends BaseController {

    private final IYzlSafetyCameraService yzlSafetyCameraService;


    @GetMapping("/ccc")
    public String ccc() {
        return "kkkkkkkkkk";
    }


    /**
     * 获取萤石云授权码 AccessToken
     * @param appKey
     * @param appSecret
     * @return
     */
    @GetMapping(value = "/getAccessToken")
    public R<String> getAccessToken(String appKey,String appSecret) {
        return R.ok(yzlSafetyCameraService.getAccessToken(appKey,appSecret));
    }


    /**
     * 获取录像机下的播放列表
     * @param accessToken
     * @param deviceSerial
     * @return
     */
    @GetMapping(value = "/cameraList")
    R<JSONObject> cameraList(String accessToken, String deviceSerial) {
        JSONObject entries = yzlSafetyCameraService.cameraList(accessToken,  deviceSerial);
        return R.ok(entries);
    }

    /**
     * (分页)获取录像机下的播放列表
     * @param accessToken
     * @param deviceSerial
     * @return
     */
    @GetMapping(value = "/cameraListPage")
    TableDataInfo<YzlYs7CameraVo> cameraListPage(String accessToken, String deviceSerial,PageQuery pageQuery) {
        return yzlSafetyCameraService.cameraListPage(accessToken,  deviceSerial,pageQuery);
    }

    /**
     * 查询摄像头视频列表
     */
    @SaCheckPermission("system:safetyCamera:list")
    @GetMapping("/list")
    public TableDataInfo<YzlSafetyCameraVo> list(YzlSafetyCameraBo bo, PageQuery pageQuery) {
        return yzlSafetyCameraService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出摄像头视频列表
     */
    @SaCheckPermission("system:safetyCamera:export")
    @Log(title = "摄像头视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(YzlSafetyCameraBo bo, HttpServletResponse response) {
        List<YzlSafetyCameraVo> list = yzlSafetyCameraService.queryList(bo);
        ExcelUtil.exportExcel(list, "摄像头视频", YzlSafetyCameraVo.class, response);
    }

    /**
     * 获取摄像头视频详细信息
     *
     * @param id 主键
     */
    /*@SaCheckPermission("system:safetyCamera:query")
    @GetMapping("/{id}")
    public R<YzlSafetyCameraVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(yzlSafetyCameraService.queryById(id));
    }*/

    /**
     * 新增摄像头视频
     */
    @SaCheckPermission("system:safetyCamera:add")
    @Log(title = "摄像头视频", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody YzlSafetyCameraBo bo) {
        bo.setDeptId(LoginHelper.getDeptId());
        bo.setUserId(LoginHelper.getUserId());
        return toAjax(yzlSafetyCameraService.insertByBo(bo));
    }

    /**
     * 修改摄像头视频
     */
    @SaCheckPermission("system:safetyCamera:edit")
    @Log(title = "摄像头视频", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody YzlSafetyCameraBo bo) {
        return toAjax(yzlSafetyCameraService.updateByBo(bo));
    }

    /**
     * 删除摄像头视频
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:safetyCamera:remove")
    @Log(title = "摄像头视频", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(yzlSafetyCameraService.deleteWithValidByIds(List.of(ids), true));
    }
}
